#include "stdafx.h"
#include "Translator.h"


CodeMood::Translator::Translator(string infilename)
{
	// Cannot use delegating constructor because we need to get outfilename first
	// and outfilename cannot be gotten by one statement.
	// Also, delegating constructor is not supported by VS11.
	string::size_type lastdot = infilename.find_last_of(".");
	string outfilename = infilename.substr(0, lastdot) + "_OUT.txt";
	init(infilename, outfilename);
}


CodeMood::Translator::Translator(string infilename, string outfilename)
{
	init(infilename, outfilename);
}


CodeMood::Translator::~Translator()
{
	// Don't do this, otherwise copy control will fail.
	// When shared_ptr is destroyed, ifstream and ofstream are
	// automatically destructed (depending on reference counting),
	// where ifstream and ofstream are closed.
	// No resource leak and elegant.
	//spif->close();
	//spof->close();
}

void CodeMood::Translator::init(string infilename, string outfilename)
{
	spif_ = make_shared<ifstream>(infilename.c_str());
	spof_ = make_shared<ofstream>(outfilename.c_str());
}
